// data_pool.js

var ctrl_inf={
  ConnectedDeviceId :  '',
  ConnectedServiceID : '',
  ConnectedCharacterID : ''
} 
export {ctrl_inf}

/**
 * 
 * @param {*} data 数据
 * @param {*} mode 模式 0 笛卡尔空间 1 关节空间
 */
function SendData(data, mode){
  var msg = mode.toString() + data 
  // 转字符串进行发送
  var buf = new ArrayBuffer(msg.length * 2); // 每个字符占用2个字节
  var bufView = new Uint16Array(buf);
  for (var i = 0, strLen = msg.length; i < strLen; i++) {
    bufView[i] = msg.charCodeAt(i);
  }
  console.log('要发送的信息是：'+ msg)
  console.log(msg)
  
  // 发送数据
  wx.writeBLECharacteristicValue({
    deviceId: this.ctrl_inf.ConnectedDeviceId,
    serviceId: this.ctrl_inf.ConnectedServiceID,
    characteristicId: this.ctrl_inf.ConnectedCharacterID,
    // 这里的value是ArrayBuffer类型
    value: bufView.buffer,
    success: function (res) {
      console.log('写入成功', res.errMsg)
      wx.showToast({
        title: '写入成功',
      })
    },
    fail(res){
      console.log(res.errCode)
      console.log('写入失败', res.errMsg)    
      wx.showToast({
        title: '写入失败',
        icon: 'error'
      })
    }
  })    
}

module.exports.SendData = SendData

